package com.lanxiao.cloud.modules.oauth.controller;

import com.lanxiao.cloud.common.core.enums.MethodEnum;
import com.lanxiao.cloud.common.core.enums.OperationType;
import com.lanxiao.cloud.common.core.enums.StateEnum;
import com.lanxiao.cloud.common.web.invalid.Log;
import com.lanxiao.cloud.common.web.service.impl.DefaultContentParse;
import com.lanxiao.cloud.modules.oauth.application.PermissionCmdManager;
import com.lanxiao.cloud.modules.oauth.application.PermissionQueryManager;
import com.lanxiao.cloud.modules.oauth.application.dto.PermissionCmdAdd;
import com.lanxiao.cloud.modules.oauth.application.dto.PermissionCmdUpdate;
import com.lanxiao.cloud.modules.oauth.application.dto.PermissionDto;
import com.lanxiao.cloud.modules.oauth.application.dto.PermissionTreeDto;
import com.lanxiao.cloud.modules.oauth.application.dto.RolePermissionResultDto;
import com.lanxiao.cloud.modules.oauth.application.dto.RouteDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2021-09-15
 */
@Api(tags = "权限")
@Validated
@RequestMapping("/oauths/permissions")
@RestController
public class PermissionController {

  private final PermissionQueryManager queryManager;
  private final PermissionCmdManager cmdManager;


  public PermissionController(
      PermissionQueryManager queryManager,
      PermissionCmdManager cmdManager) {
    this.queryManager = queryManager;
    this.cmdManager = cmdManager;
  }

  @ApiOperation(value = "条件查询权限集合-结构树")
  @GetMapping("/tree")
  public List<PermissionTreeDto> treeQuery() {
    return queryManager.treeQuery();
  }

  @ApiOperation(value = "查询非按钮类型的权限集合")
  @GetMapping("/not-button")
  public List<PermissionDto> queryNotButtonType() {
    return queryManager.queryNotButtonType();
  }

  @ApiOperation(value = "通过角色ID查询权限ID集合")
  @GetMapping("/role")
  public RolePermissionResultDto rolePermissionQuery(@RequestParam(required = false) Long roleId) {
    return queryManager.rolePermissionQuery(roleId);
  }

  @ApiOperation(value = "登录用户路由资源列表-结构树")
  @GetMapping("/user/route")
  public List<RouteDto> userRoute() {
    return queryManager.userRoute();
  }

  @ApiOperation(value = "新增权限")
  @Log(operationType = OperationType.CREATE)
  @PostMapping
  public ResponseEntity<Void> save(@Valid @RequestBody PermissionCmdAdd add) {
    cmdManager.savePermission(add);
    return ResponseEntity.status(HttpStatus.CREATED).build();
  }

  @ApiOperation(value = "修改权限")
  @Log(operationType = OperationType.UPDATE,
      serviceClass = PermissionQueryManager.class)
  @PutMapping("/{id}")
  public ResponseEntity<Void> update(@PathVariable Long id,
      @Valid @RequestBody PermissionCmdUpdate update) {
    cmdManager.updatePermission(id, update);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

  @ApiOperation(value = "删除权限")
  @Log(operationType = OperationType.DELETE)
  @DeleteMapping("/{id}")
  public ResponseEntity<Void> delete(@PathVariable Long id) {
    cmdManager.deletePermission(id);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

  @ApiOperation(value = "校验权限标识是否重复")
  @GetMapping("/check/mark")
  public ResponseEntity<Void> checkMark(Long id, String mark) {
    cmdManager.checkMark(id, mark);
    return ResponseEntity.ok().build();
  }

  @ApiOperation(value = "校验URL地址是否重复")
  @GetMapping("/check/url")
  public ResponseEntity<Void> checkUrl(Long id, String url, MethodEnum method) {
    cmdManager.checkUrl(id, url, method);
    return ResponseEntity.ok().build();
  }

  @ApiOperation(value = "校验路由地址是否重复")
  @GetMapping("/check/path")
  public ResponseEntity<Void> checkPath(Long id, String path) {
    cmdManager.checkPath(id, path);
    return ResponseEntity.ok().build();
  }

}
